import turtle

# 步长
step = 100
width = step * 2 * 6
height = width * 2 / 3

# 设置画布
screen = turtle.getscreen()
screen.title("笛卡儿坐标系（Cartesian coordinate system），也称直角坐标系（rectangular coordinate system）")
screen.setup(width + 200, height + 200)

# 设置画笔
pen = turtle.getpen()
pen.shape("turtle")
pen.speed(0) # 速度放到最快

# 画x轴
pen.teleport(0 - width / 2, 0)
pen.setheading(0)
pen.forward(width)
# 画箭头
pen.left(150)
pen.forward(10)
pen.write("x", align="center", font=("Arial", 32, "italic"))
pen.backward(10)
pen.right(300)
pen.forward(10)

# 画y轴
pen.teleport(0, 0 - height / 2)
pen.setheading(90)
pen.forward(height)
# 画箭头
pen.left(150)
pen.forward(10)
pen.write("y", align="center", font=("Arial", 32, "italic"))
pen.backward(10)
pen.right(300)
pen.forward(10)

# 参考线数量
x_lines = int(height / 2 / step)
y_lines = int(width / 2 / step)

## 画参考线
pen.color("snow2")
# 画横向参考线
pen.setheading(0)
for i in range(-x_lines + 1, x_lines):
    if i == 0:
        continue
    pen.teleport(0 - width / 2, i * step)
    pen.forward(width)
# 画纵向参考线
pen.setheading(90)
for i in range(-y_lines + 1, y_lines):
    if i == 0:
        continue
    pen.teleport(i * step, 0 - height / 2)
    pen.forward(height)

# 标记象限
def lable_quadrant():
    pen.color("snow2")
    pen.teleport(width / 2 / 2, height / 2 / 2 + 24)
    pen.write("I", align="center", font=("Arial", 42, "bold"))
    pen.teleport(width / 2 / 2, height / 2 / 2 - 24)
    pen.write("(正, 正)", align="center", font=("Arial", 36, "bold"))
    pen.teleport(- width / 2 / 2, height / 2 / 2 + 24)
    pen.write("Ⅱ", align="center", font=("Arial", 42, "bold"))
    pen.teleport(- width / 2 / 2, height / 2 / 2 - 24)
    pen.write("(正, 负)", align="center", font=("Arial", 36, "bold"))
    pen.teleport(- width / 2 / 2, - height / 2 / 2 + 24)
    pen.write("Ⅲ", align="center", font=("Arial", 42, "bold"))
    pen.teleport(- width / 2 / 2, - height / 2 / 2 - 24)
    pen.write("(负, 负)", align="center", font=("Arial", 36, "bold"))
    pen.teleport(width / 2 / 2, - height / 2 / 2 + 24)
    pen.write("Ⅳ", align="center", font=("Arial", 42, "bold"))
    pen.teleport(width / 2 / 2, - height / 2 / 2 - 24)
    pen.write("(正, 负)", align="center", font=("Arial", 36, "bold"))
# 标记象限
lable_quadrant()

# 画x标尺
def draw_x_scale(x):
    pen.teleport(x * step, -5)
    pen.color("black")
    pen.setheading(0)
    pen.left(90)
    pen.forward(10)
    pen.teleport(x * step, -52)
    pen.write(x, align="center", font=("Arial", 32, "normal"))

# 画x轴标尺
for i in range(- y_lines + 1, y_lines):
    draw_x_scale(i)

# 画y标尺
def draw_y_scale(y):
    pen.teleport(5, y * step)
    pen.color("black")
    pen.setheading(90)
    pen.left(90)
    pen.forward(10)
    pen.teleport(-24, y * step - 18)
    pen.write(y, align="center", font=("Arial", 32, "normal"))
# 画y轴标尺
for i in range(- x_lines + 1, x_lines):
    draw_y_scale(i)

# 画点
def draw_point(x, y):
    # 设置颜色
    if x > 0 and y > 0:
        pen.pencolor("blue")
    elif x < 0 and y > 0:
        pen.pencolor("green")
    elif x < 0 and y < 0:
        pen.pencolor("red")
    elif x > 0 and y < 0:
        pen.pencolor("sienna")
    else:
        pen.pencolor("purple")
    # 找x轴坐标
    pen.teleport(x * step, 0)
    pen.dot()
    pen.write(x, align="center", font=("Arial", 24, "normal"))
    pen.setheading(90)
    if y >= 0:
        dot_step = 5
    else:
        dot_step = -5
    for i in range(int(y * step / dot_step)):
        if i % 2 == 0:
            pen.down()
        else:
            pen.up()
        pen.forward(dot_step)
    # 找y轴坐标
    pen.teleport(0, y * step)
    pen.dot()
    pen.write(y, align="center", font=("Arial", 24, "normal"))
    pen.setheading(0)
    if x >= 0:
        dot_step = 5
    else:
        dot_step = -5
    for i in range(int(x * step / dot_step)):
        if i % 2 == 0:
            pen.down()
        else:
            pen.up()
        pen.forward(dot_step)
    # 坐标定点
    pen.teleport(x * step, y * step)
    pen.dot(10)
    if y >= 0:
        if x == 0:
            pen.teleport(x * step + 48, y * step + 8)
        else:
            pen.teleport(x * step, y * step + 8)
    else:
        if x == 0:
            pen.teleport(x * step + 48, y * step - 48)
        else:
            pen.teleport(x * step, y * step - 48)
    pen.write("("+str(x)+", "+str(y)+")", align="center", font=("Arial", 36, "normal"))
    # 回到原点
    pen.teleport(0, 0)
    pen.setheading(0)

draw_point(2, 3.14)
draw_point(-4.25, 1.618)
draw_point(-2.5, -2.718)
draw_point(4.75, -1.414)
draw_point(0, 0)

# 收笔
pen.hideturtle()
# 持续显示
screen.mainloop()